﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using TestPrint.PrintHelp;
using TestPrint.PrintNameHelp;

namespace TestPrint
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            
        }

        public List<string> OpenFile(string filter = "*|*.*")
        {
            List<string> files = new List<string>();
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Title = "最多上传五个文件";
                ofd.Filter = filter;
                ofd.Multiselect = true;
                if (ofd.ShowDialog() == DialogResult.Cancel)
                    return files;
                files = ofd.FileNames.ToList();
            }
            return files;
        }

        public byte[] GetFileByte(string file)
        {
            //FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            //byte[] infbytes = new byte[(int)fs.Length];
            //fs.Read(infbytes, 0, infbytes.Length);
            //fs.Close();
            //return infbytes;
            Stream sm = new FileStream(file, FileMode.Open, FileAccess.Read);
            var by = StreamToBytes(sm);
            sm.Close();
            return by;
        }

        public byte[] StreamToBytes(Stream stream)
        {
            MemoryStream ms = new MemoryStream();
            byte[] buffer = new byte[1024];
            while (true)
            {
                int sz = stream.Read(buffer, 0, 1024);
                if (sz == 0) break;
                ms.Write(buffer, 0, sz);
            }
            ms.Position = 0;
            byte[] byData = ms.ToArray();
            ms.Close();
            return byData;
            //byte[] bytes = new byte[stream.Length];
            //stream.Read(bytes, 0, bytes.Length);
            //// 设置当前流的位置为流的开始 
            //stream.Seek(0, SeekOrigin.Begin);
            //return bytes;
        }

        private void btnSetPrintName_Click(object sender, EventArgs e)
        {
            LocalPrinterSetting.ShowUI();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            List<string> files = OpenFile("*.JPG; *.PNG; *.PDF| *.JPG; *.PNG; *.PDF");
            if (files == null || files.Count <= 0)
            {
                MessageBox.Show("请先选择要上传的文件", "提示");
                return;
            }
            if (files.Count() > 5)
            {
                MessageBox.Show("一次最多只能上传5个文件", "提示");
                return;
            }
            txtFilePath.Text = string.Join("\r\n", files);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtFilePath.Text))
            {

                List<OutputImage> OutputImages = new List<OutputImage>();
                foreach (var it in txtFilePath.Lines)
                {
                    List<byte[]> Images = new List<byte[]>();
                    Images.Add(GetFileByte(it));
                    var data = new OutputImage()
                    {
                        ImageType = Path.GetExtension(it).Replace(".", "").ToUpper(),
                        PageHeight = 1169,
                        PageWidth = 827,
                        Images = Images
                    };
                    OutputImages.Add(data);
                }
                using (PrintHelper print = new PrintHelper())
                {
                    print.Print(OutputImages);
                }
            }
        }
    }
}
